package com.example.asus.homework11.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;


import com.example.asus.homework11.R;
import com.example.asus.homework11.bean.CommentBean;

import java.util.List;


public class UserOrderAdapter extends RecyclerView.Adapter<UserOrderAdapter.MyViewHolder> {

    private List<CommentBean> beans;
    private OnItemClickListener onItemClickListener;

    public interface OnItemClickListener{
        void setItem(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    public void setData(List<CommentBean> beans){
        this.beans = beans;
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new MyViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_order,viewGroup,false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, final int i) {
        final CommentBean bean = beans.get(i);
        final int position = i;
        myViewHolder.tv_foodname.setText(bean.getFoodname());
        myViewHolder.tv_ordernum.setText(bean.getNum()+"");
        myViewHolder.tv_shopname.setText(bean.getShopname());
        myViewHolder.tv_time.setText(bean.getOrder_time());
        myViewHolder.tv_price.setText(bean.getPrice()+"元");
        if(onItemClickListener!=null){
            myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.setItem(v,position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return beans==null?0:beans.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        public View itemView;
        private TextView tv_foodname,tv_ordernum,tv_shopname,tv_time,tv_price;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            this.itemView=itemView;
            tv_price=itemView.findViewById(R.id.tv_price);
            tv_foodname=itemView.findViewById(R.id.tv_foodname);
            tv_ordernum=itemView.findViewById(R.id.tv_ordernum);
            tv_shopname=itemView.findViewById(R.id.tv_shopname);
            tv_time=itemView.findViewById(R.id.tv_time);
        }
    }


}
